package com.jml.mapreduce.分组比较器;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

/**
 *封装OrderBean，输出key就是OrderBean，没有value，没有value就是用NullWritable，
 * NullWritable：表示啥也没有
 */
public class OrderMapper  extends Mapper<LongWritable, Text, OrderBean, NullWritable> {

    private OrderBean order = new OrderBean();

    /**
     * Map用来封装OrderBean
     * @param key
     * @param value
     * @param context
     * @throws IOException
     * @throws InterruptedException
     */
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        //拆分
        String[] fields = value.toString().split("\t");
        //封装
        order.setOrderId(fields[0]);
        order.setProductId(fields[1]);
        order.setPrice(Double.parseDouble(fields[2]));
        //什么都没有就用NullWritable
        context.write(order, NullWritable.get());
    }

    /*
    或者是
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        super.map(key, value, context);
    }
    */
}
